package com.arellomobile.android.push;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import com.arellomobile.android.push.exception.PushWooshException;
import com.arellomobile.android.push.preference.SoundType;
import com.arellomobile.android.push.preference.VibrateType;
import com.arellomobile.android.push.tags.SendPushTagsAsyncTask;
import com.arellomobile.android.push.tags.SendPushTagsCallBack;
import com.arellomobile.android.push.utils.GeneralUtils;
import com.arellomobile.android.push.utils.PreferenceUtils;
import com.arellomobile.android.push.utils.WorkerTask;
import com.arellomobile.android.push.utils.executor.ExecutorHelper;
import com.google.android.gcm.GCMRegistrar;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class PushManager
{
  private static final String HTML_URL_FORMAT = "https://cp.pushwoosh.com/content/%s";
  public static final String PUSH_RECEIVE_EVENT = "PUSH_RECEIVE_EVENT";
  public static final String REGISTER_BROAD_CAST_ACTION = "com.arellomobile.android.push.REGISTER_BROAD_CAST_ACTION";
  public static final String REGISTER_ERROR_EVENT = "REGISTER_ERROR_EVENT";
  public static final String REGISTER_EVENT = "REGISTER_EVENT";
  public static final String UNREGISTER_ERROR_EVENT = "UNREGISTER_ERROR_EVENT";
  public static final String UNREGISTER_EVENT = "UNREGISTER_EVENT";
  private static AsyncTask<Void, Void, Void> mRegistrationAsyncTask;
  static volatile String mSenderId;
  private static final Object mSyncObj = new Object();
  private volatile String mAppId;
  private Context mContext;
  private Bundle mLastBundle;

  PushManager(Context paramContext)
  {
    GeneralUtils.checkNotNull(paramContext, "context");
    this.mContext = paramContext;
    this.mAppId = PreferenceUtils.getApplicationId(paramContext);
    mSenderId = PreferenceUtils.getSenderId(paramContext);
  }

  public PushManager(Context paramContext, String paramString1, String paramString2)
  {
    this(paramContext);
    this.mAppId = paramString1;
    mSenderId = paramString2;
    PreferenceUtils.setApplicationId(paramContext, this.mAppId);
    PreferenceUtils.setSenderId(paramContext, paramString2);
  }

  private void cancelPrevRegisterTask()
  {
    synchronized (mSyncObj)
    {
      if (mRegistrationAsyncTask != null)
        mRegistrationAsyncTask.cancel(true);
      mRegistrationAsyncTask = null;
      return;
    }
  }

  public static void clearLocalNotifications(Context paramContext)
  {
    AlarmReceiver.clearAlarm(paramContext);
  }

  private AsyncTask<Void, Void, Void> getRegisterAsyncTask(Context paramContext, final String paramString)
  {
    try
    {
      WorkerTask local6 = new WorkerTask(paramContext)
      {
        protected void doWork(Context paramAnonymousContext)
        {
          DeviceRegistrar.registerWithServer(PushManager.this.mContext, paramString);
        }
      };
      return local6;
    }
    catch (Throwable localThrowable)
    {
      DeviceRegistrar.registerWithServer(paramContext, paramString);
    }
    return null;
  }

  private boolean neededToRequestPushWooshServer(Context paramContext)
  {
    Calendar localCalendar1 = Calendar.getInstance();
    Calendar localCalendar2 = Calendar.getInstance();
    localCalendar2.add(12, -10);
    Calendar localCalendar3 = Calendar.getInstance();
    localCalendar3.setTime(new Date(PreferenceUtils.getLastRegistration(paramContext)));
    return (!localCalendar2.before(localCalendar3)) || (!localCalendar3.before(localCalendar1));
  }

  private void registerOnPushWoosh(Context paramContext, String paramString)
  {
    cancelPrevRegisterTask();
    mRegistrationAsyncTask = getRegisterAsyncTask(paramContext, paramString);
    ExecutorHelper.executeAsyncTask(mRegistrationAsyncTask);
  }

  public static void scheduleLocalNotification(Context paramContext, String paramString, int paramInt)
  {
    scheduleLocalNotification(paramContext, paramString, null, paramInt);
  }

  public static void scheduleLocalNotification(Context paramContext, String paramString, Bundle paramBundle, int paramInt)
  {
    AlarmReceiver.setAlarm(paramContext, paramString, paramBundle, paramInt);
  }

  private void sendAppOpen(Context paramContext)
  {
    try
    {
      WorkerTask local4 = new WorkerTask(paramContext)
      {
        protected void doWork(Context paramAnonymousContext)
        {
          DeviceFeature2_5.sendAppOpen(paramAnonymousContext);
        }
      };
      ExecutorHelper.executeAsyncTask(local4);
      return;
    }
    catch (Throwable localThrowable)
    {
      DeviceFeature2_5.sendAppOpen(paramContext);
    }
  }

  public static void sendGoalAchieved(Context paramContext, final String paramString, final Integer paramInteger)
  {
    try
    {
      WorkerTask local5 = new WorkerTask(paramContext)
      {
        protected void doWork(Context paramAnonymousContext)
        {
          DeviceFeature2_5.sendGoalAchieved(paramAnonymousContext, paramString, paramInteger);
        }
      };
      ExecutorHelper.executeAsyncTask(local5);
      return;
    }
    catch (Throwable localThrowable)
    {
      DeviceFeature2_5.sendGoalAchieved(paramContext, paramString, paramInteger);
    }
  }

  public static void sendLocation(Context paramContext, final Location paramLocation)
  {
    if (!GCMRegistrar.isRegisteredOnServer(paramContext))
      return;
    try
    {
      WorkerTask local2 = new WorkerTask(paramContext)
      {
        protected void doWork(Context paramAnonymousContext)
        {
          try
          {
            DeviceFeature2_5.getNearestZone(paramAnonymousContext, paramLocation);
            return;
          }
          catch (Exception localException)
          {
          }
        }
      };
      ExecutorHelper.executeAsyncTask(local2);
      return;
    }
    catch (Throwable localThrowable)
    {
      try
      {
        DeviceFeature2_5.getNearestZone(paramContext, paramLocation);
        return;
      }
      catch (Exception localException)
      {
      }
    }
  }

  public static void sendTags(Context paramContext, final Map<String, Object> paramMap, final SendPushTagsCallBack paramSendPushTagsCallBack)
  {
    new Handler(paramContext.getMainLooper()).post(new Runnable()
    {
      public void run()
      {
        SendPushTagsAsyncTask localSendPushTagsAsyncTask = new SendPushTagsAsyncTask(PushManager.this, paramSendPushTagsCallBack);
        Map[] arrayOfMap = new Map[1];
        arrayOfMap[0] = paramMap;
        localSendPushTagsAsyncTask.execute(arrayOfMap);
      }
    });
  }

  public static Map<String, String> sendTagsFromBG(Context paramContext, Map<String, Object> paramMap)
    throws PushWooshException
  {
    HashMap localHashMap = new HashMap();
    try
    {
      JSONArray localJSONArray = DeviceFeature2_5.sendTags(paramContext, paramMap);
      for (int i = 0; ; i++)
      {
        if (i >= localJSONArray.length())
          return localHashMap;
        JSONObject localJSONObject = localJSONArray.getJSONObject(i);
        localHashMap.put(localJSONObject.getString("tag"), localJSONObject.getString("reason"));
      }
    }
    catch (Exception localException)
    {
      throw new PushWooshException(localException);
    }
  }

  public static void sendTagsFromUI(Context paramContext, Map<String, Object> paramMap, SendPushTagsCallBack paramSendPushTagsCallBack)
  {
    new SendPushTagsAsyncTask(paramContext, paramSendPushTagsCallBack).execute(new Map[] { paramMap });
  }

  public static void setEnableLED(Context paramContext, boolean paramBoolean)
  {
    PreferenceUtils.setEnableLED(paramContext, paramBoolean);
  }

  public static void setLightScreenOnNotification(Context paramContext, boolean paramBoolean)
  {
    PreferenceUtils.setLightScreenOnNotification(paramContext, paramBoolean);
  }

  public static void setMultiNotificationMode(Context paramContext)
  {
    PreferenceUtils.setMultiMode(paramContext, true);
  }

  public static void setSimpleNotificationMode(Context paramContext)
  {
    PreferenceUtils.setMultiMode(paramContext, false);
  }

  public static void setSoundNotificationType(Context paramContext, SoundType paramSoundType)
  {
    PreferenceUtils.setSoundType(paramContext, paramSoundType);
  }

  public static void setVibrateNotificationType(Context paramContext, VibrateType paramVibrateType)
  {
    PreferenceUtils.setVibrateType(paramContext, paramVibrateType);
  }

  Context getContext()
  {
    return this.mContext;
  }

  public String getCustomData()
  {
    if (this.mLastBundle == null)
      return null;
    return this.mLastBundle.getString("u");
  }

  boolean onHandlePush(Activity paramActivity)
  {
    Bundle localBundle = paramActivity.getIntent().getBundleExtra("pushBundle");
    if ((localBundle == null) || (this.mContext == null))
      return false;
    this.mLastBundle = localBundle;
    JSONObject localJSONObject = new JSONObject();
    Iterator localIterator = localBundle.keySet().iterator();
    while (true)
    {
      if (!localIterator.hasNext())
      {
        PushEventsTransmitter.onMessageReceive(this.mContext, localJSONObject.toString(), localBundle);
        String str2 = (String)localBundle.get("h");
        if (str2 != null)
        {
          String str3 = String.format("https://cp.pushwoosh.com/content/%s", new Object[] { str2 });
          Intent localIntent = new Intent(paramActivity, PushWebview.class);
          localIntent.putExtra("url", str3);
          paramActivity.startActivity(localIntent);
        }
        ((String)localBundle.get("l"));
        sendPushStat(this.mContext, localBundle.getString("p"));
        return true;
      }
      String str1 = (String)localIterator.next();
      if (str1.equals("u"));
      try
      {
        localJSONObject.put("userdata", localBundle.get("u"));
        try
        {
          label199: localJSONObject.put(str1, localBundle.get(str1));
        }
        catch (JSONException localJSONException1)
        {
        }
      }
      catch (JSONException localJSONException2)
      {
        break label199;
      }
    }
  }

  public void onStartup(Context paramContext)
  {
    onStartup(paramContext, true);
  }

  public void onStartup(Context paramContext, boolean paramBoolean)
  {
    GeneralUtils.checkNotNullOrEmpty(this.mAppId, "mAppId");
    GeneralUtils.checkNotNullOrEmpty(mSenderId, "mSenderId");
    GCMRegistrar.checkDevice(paramContext);
    GCMRegistrar.checkManifest(paramContext);
    if (paramBoolean)
      sendAppOpen(paramContext);
    String str = GCMRegistrar.getRegistrationId(paramContext);
    if (str.equals(""))
    {
      arrayOfString = new String[1];
      arrayOfString[0] = mSenderId;
      GCMRegistrar.register(paramContext, arrayOfString);
    }
    while (((paramContext instanceof Activity)) && (((Activity)paramContext).getIntent().hasExtra("PUSH_RECEIVE_EVENT")))
    {
      String[] arrayOfString;
      return;
    }
    if (!PreferenceUtils.getApplicationId(paramContext).equals(this.mAppId))
    {
      registerOnPushWoosh(paramContext, str);
      return;
    }
    if (neededToRequestPushWooshServer(paramContext))
    {
      registerOnPushWoosh(paramContext, str);
      return;
    }
    PushEventsTransmitter.onRegistered(paramContext, str);
  }

  void sendPushStat(Context paramContext, final String paramString)
  {
    try
    {
      WorkerTask local3 = new WorkerTask(paramContext)
      {
        protected void doWork(Context paramAnonymousContext)
        {
          DeviceFeature2_5.sendPushStat(paramAnonymousContext, paramString);
        }
      };
      ExecutorHelper.executeAsyncTask(local3);
      return;
    }
    catch (Throwable localThrowable)
    {
      DeviceFeature2_5.sendPushStat(paramContext, paramString);
    }
  }

  public void startTrackingGeoPushes()
  {
    this.mContext.startService(new Intent(this.mContext, GeoLocationService.class));
  }

  public void stopTrackingGeoPushes()
  {
    this.mContext.stopService(new Intent(this.mContext, GeoLocationService.class));
  }

  public void unregister()
  {
    cancelPrevRegisterTask();
    GCMRegistrar.unregister(this.mContext);
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.arellomobile.android.push.PushManager
 * JD-Core Version:    0.6.2
 */